package com.xqzgj.system.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xqzgj.common.annotation.Log;
import com.xqzgj.common.core.controller.BaseController;
import com.xqzgj.common.core.domain.AjaxResult;
import com.xqzgj.common.enums.BusinessType;
import com.xqzgj.common.constant.UserConstants;
import com.xqzgj.system.domain.StockSupplier;
import com.xqzgj.system.service.IStockSupplierService;
import com.xqzgj.common.utils.poi.ExcelUtil;
import com.xqzgj.common.core.page.TableDataInfo;

/**
 * 供应商信息Controller
 */
@RestController
@RequestMapping("/system/supplier")
public class StockSupplierController extends BaseController {
    @Autowired
    private IStockSupplierService stockSupplierService;

    /**
     * 查询供应商信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockSupplier stockSupplier) {
        startPage();
        List<StockSupplier> list = stockSupplierService.selectStockSupplierList(stockSupplier);
        return getDataTable(list);
    }

    /**
     * 导出供应商信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:export')")
    @Log(title = "供应商信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StockSupplier stockSupplier) {
        List<StockSupplier> list = stockSupplierService.selectStockSupplierList(stockSupplier);
        ExcelUtil<StockSupplier> util = new ExcelUtil<StockSupplier>(StockSupplier.class);
        return util.exportExcel(list, "供应商信息数据");
    }

    /**
     * 获取供应商信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stockSupplierService.selectStockSupplierById(id));
    }

    /**
     * 新增供应商信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:add')")
    @Log(title = "供应商信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockSupplier stockSupplier) {
        if (!stockSupplierService.checkSupplierCodeUnique(stockSupplier)) {
            return error("新增供应商'" + stockSupplier.getSupplierCode() + "'失败，供应商编码已存在");
        }
        if (!stockSupplierService.checkSupplierNameUnique(stockSupplier)) {
            return error("新增供应商'" + stockSupplier.getSupplierName() + "'失败，供应商名称已存在");
        }
        return toAjax(stockSupplierService.insertStockSupplier(stockSupplier));
    }

    /**
     * 修改供应商信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:edit')")
    @Log(title = "供应商信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockSupplier stockSupplier) {
        if (!stockSupplierService.checkSupplierCodeUnique(stockSupplier)) {
            return error("修改供应商'" + stockSupplier.getSupplierCode() + "'失败，供应商编码已存在");
        }
        if (!stockSupplierService.checkSupplierNameUnique(stockSupplier)) {
            return error("修改供应商'" + stockSupplier.getSupplierName() + "'失败，供应商名称已存在");
        }
        return toAjax(stockSupplierService.updateStockSupplier(stockSupplier));
    }

    /**
     * 删除供应商信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:remove')")
    @Log(title = "供应商信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(stockSupplierService.deleteStockSupplierByIds(ids));
    }

    /**
     * 更新供应商状态
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:edit')")
    @Log(title = "供应商信息", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestBody StockSupplier stockSupplier) {
        return toAjax(stockSupplierService.updateStockSupplierStatus(stockSupplier));
    }
}